package hlhj.fhp.supreme.adapters

import android.app.Activity
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.support.v4.app.FragmentActivity
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.mylhyl.superdialog.SuperDialog
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.activitys.ArticleEditAty
import hlhj.fhp.supreme.activitys.MyJiePanAty
import hlhj.fhp.supreme.javabean.ArticleListBean
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils

/**
 * Created by Never Fear   on 2018\7\17 0017.
Never More....
 */
class MyJiePanAdp(datas:ArrayList<ArticleListBean.DataBean>,private val activity: Activity,private val fac:MyJiePanAty) :BaseQuickAdapter<ArticleListBean.DataBean,BaseViewHolder>(R.layout.myjiepan_item,datas) {
    override fun convert(helper: BaseViewHolder?, item: ArticleListBean.DataBean?) {
        helper?.setText(R.id.itemTittle,item?.title)
        helper?.setText(R.id.itemType,item?.create_time)
        helper?.setText(R.id.itemTime,item?.des)
        Glide.with(mContext).load(item?.img).asBitmap().error(R.mipmap.logo).into(helper?.getView(R.id.itemIcon))
        helper?.getView<TextView>(R.id.btEdit)!!
                .setOnClickListener {
                    val intent = Intent(mContext, ArticleEditAty::class.java)
                    intent.putExtra("id",item?.id.toString())
                    intent.putExtra("des",item?.des)
                    intent.putExtra("cid",item?.cat_id!!.toInt())
                    intent.putExtra("title",item?.title)
                    mContext.startActivity(intent)
                }
        helper?.getView<TextView>(R.id.btDelet)!!
                .setOnClickListener {
                    AlertDialog.Builder(mContext)
                            .setTitle("确定删除解盘？")
                            .setPositiveButton("确定") { dialog, which ->
                                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.DELET_ART)
                                        .params("id",item?.id)
                                        .execute(object :JsonCallBack<BaseBean>(activity){
                                            override fun onSuccess(response: Response<BaseBean>?) {
                                                val baseBean = response!!.body()
                                                if (baseBean.code==1){
                                                    data.removeAt(helper?.adapterPosition)
                                                    notifyDataSetChanged()
                                                    dialog?.dismiss()
                                                    MyUtils.toast("删除成功")
                                                }else{
                                                    MyUtils.toast(baseBean.msg)
                                                }
                                            }
                                        })
                            }
                            .setNegativeButton("取消"){
                                dialog, which ->
                                dialog.dismiss()
                            }
                            .show()

                }




    }
}